package com.example.controller;

import com.example.entity.Merchant;
import com.example.entity.User;
import com.example.entity.indent;
import com.example.entity.menu;
import com.example.service.AdminService;
import com.example.service.IndentService;
import com.example.service.MenuService;
import com.example.service.MerchantService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;

@Controller
@RequestMapping("admin")
public class AdminController {
      private static  final Logger log = LoggerFactory.getLogger(UserController.class);
      private AdminService adminService;
      private MenuService menuService;
      private IndentService indentService;
      private MerchantService merchantService;

      @Autowired
      public AdminController(AdminService adminService, MenuService menuService, IndentService indentService, MerchantService merchantService) {
            this.adminService = adminService;
            this.menuService = menuService;
            this.indentService = indentService;
            this.merchantService = merchantService;
      }


      @RequestMapping("userLists")
      public String userLists(Model model){
            List<User> userLists = adminService.findAll();
            model.addAttribute("userLists",userLists);
            return "adminUserLists";
      }

      @RequestMapping("delete")
      public String userLists(Integer id){
            log.debug("删除用户的ID:{}",id);
            adminService.deleteUser(id);
            return "redirect:/admin/userLists";
      }
      @RequestMapping("merchantLists")
      public String merchantLists(Model model){
            List<Merchant> merchantLists = adminService.findAllMer();
            model.addAttribute("merchantLists",merchantLists);
            return "adminMerchantLists";
      }
      @RequestMapping("menuLists")
      public String menuLists(Model model){
            List<Merchant> menuLists = adminService.findAllMenu();
            model.addAttribute("menuLists",menuLists);
            return "adminMenuLists";
      }
      @RequestMapping("deleteMenu")
      public String deleteMenu(Integer idpic, Model model, menu menu){

            log.debug("需要删除的菜品的ID：{}",idpic);

            String photo=menuService.findMenuByID(idpic).getPhoto();
//            删除信息
            menuService.deleteMenuByID(idpic);
//            删除照片
            new File(menu.getPhoto(),photo).delete();
            return menuLists(model);
      }
      @RequestMapping("indentLists")
      public String findUserIndent(Model model) {

            List<indent> indentOrder = indentService.findAllIndent();

            model.addAttribute("indent", indentOrder);

            return "adminIndentLists";
      }

      @RequestMapping("register")
      public String register(Integer id,Model model){
            merchantService.updateRegister(id);
            return merchantLists(model);
      }
}
